package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.Users;
import com.zch.house.service.UsersService;
import com.zch.house.service.impl.UsersServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet2")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    UsersService us = new UsersServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("login".equals(method)){
			login(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("gubi".equals(method)){
			getUserById(request,response);
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) {
		
		
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String u_name = request.getParameter("name");
		String u_pass = request.getParameter("password");
		String u_realname = request.getParameter("realName");
		String u_sex = request.getParameter("sex");
		String roleIdStr = request.getParameter("roleIdStr");
		int role_id = Integer.parseInt(roleIdStr);
		String createTimeStr = request.getParameter("createTime");
		Date u_create_time = DateFormat.to_Date2(createTimeStr);
		String stateStr = request.getParameter("state");
		int  u_state = Integer.parseInt(stateStr);
		Users user = new Users(u_name, u_pass, u_realname, u_sex, role_id, u_create_time, u_state);
		//调用service层的方法
		boolean flag = us.addUsers(user);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='LoginServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='LoginServlet2?method=select'</script>");
		}
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = us.deleteUsers(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='LoginServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='LoginServlet2?method=select'</script>");
		}
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String u_name = request.getParameter("name");
		String u_pass = request.getParameter("password");
		String u_realname = request.getParameter("realName");
		String u_sex = request.getParameter("sex");
		String roleIdStr = request.getParameter("roleIdStr");
		int role_id = Integer.parseInt(roleIdStr);
		String createTimeStr = request.getParameter("createTime");
		Date u_create_time = DateFormat.to_Date2(createTimeStr);
		String stateStr = request.getParameter("state");
		int  u_state = Integer.parseInt(stateStr);
		Users user = new Users(id, u_name, u_pass, u_realname, u_sex, role_id, u_create_time, u_state);
		boolean flag = us.updateUsers(user);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='LoginServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='LoginServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) {
		
		
	}

	private void getUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		Users user = us.getUsersById(id);
		request.setAttribute("user", user);
		request.getRequestDispatcher("view/users/edit.jsp").forward(request, response);
	}


}
